package com.jframe.sys.utils;

import java.util.HashMap;
import java.util.Map;
import org.apache.log4j.Logger;
public class CacheFactory {
	private static Logger logger = Logger.getLogger(CacheFactory.class);
    
    private static Map<String, CacheUtil> hashMapWrapper = new HashMap<String, CacheUtil>();
         
    /**
     * 获取一个名称为cacheName的缓存对象；如果不存在，返回null
     * @param cacheName
     * @return
     */
    public static CacheUtil getCacheUtil(String cacheName){
        return hashMapWrapper.get(cacheName);
    }
         
    /**
     * 清理所有的缓存
     */
    public static void clearCache(){
        Object[] cacheArray = hashMapWrapper.keySet().toArray();
        for(int i=0, l=cacheArray.length; i<l; i++){
            String cacheName = cacheArray[i].toString();
            logger.debug("The cache is below to clear, And the name is : " + cacheName);
            CacheUtil CacheUtil = hashMapWrapper.get(cacheName);
            CacheUtil.clear();
        }
    }
         
    /**
     * 获取一个名称为cacheName的缓存对象；如果不存在，则创建一个新的缓存对象
     * @param cacheName
     * @return
     */
    private static CacheUtil createCacheUtil(String cacheName){
            return CacheUtil.getInstance(cacheName);
    }
    
    /**
     * 创建缓存对象
     * @param cacheName
     */
    private static void createHashMapWrapper(String cacheName){
        hashMapWrapper.put(cacheName, createCacheUtil(cacheName));
    }
         
    /**
     * 初始化缓存对象
     */
    public static void initCache(String cacheName){
        createHashMapWrapper(cacheName);
    }
}
